/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.catalina;


import javax.servlet.MultipartConfigElement;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.UnavailableException;


/**
 * A <b>Wrapper</b> is a Container that represents an individual servlet
 * definition from the deployment descriptor of the web application.  It
 * provides a convenient mechanism to use Interceptors that see every single
 * request to the servlet represented by this definition.
 * <p>
 * Implementations of Wrapper are responsible for managing the servlet life
 * cycle for their underlying servlet class, including calling init() and
 * destroy() at appropriate times, as well as respecting the existence of
 * the SingleThreadModel declaration on the servlet class itself.
 * <p>
 * The parent Container attached to a Wrapper will generally be an
 * implementation of Context, representing the servlet context (and
 * therefore the web application) within which this servlet executes.
 * <p>
 * Child Containers are not allowed on Wrapper implementations, so the
 * <code>addChild()</code> method should throw an
 * <code>IllegalArgumentException</code>.
 *
 * @author Craig R. McClanahan
 */
// Wrapper是一个容器，它表示来自web应用程序部署描述符的单个servlet定义。
// 它提供了一种使用拦截器的方便机制，拦截器可以查看此定义表示的对servlet的每个请求。
// Wrapper的实现负责管理其底层servlet类的servlet生命周期，包括在适当的时候调用init()和destroy()，
// 以及尊重servlet类本身存在的SingleThreadModel声明。
// 附加到Wrapper的父容器通常是Context的实现，表示servlet上下文(因此是web应用程序)，servlet在其中执行。
// 在Wrapper实现上不允许子容器，因此addChild()方法应该抛出一个IllegalArgumentException。

// 一个Engine代表一个tomcat
// 一个Host代表一个虚拟主机
// 一个Context代表一个应用
// 一个Wrapper代表一个Servlet
public interface Wrapper extends Container {

    /**
     * Container event for adding a wrapper.
     */
    // 容器事件，用于添加包装器
    String ADD_MAPPING_EVENT = "addMapping";

    /**
     * Container event for removing a wrapper.
     */
    // 容器事件，用于删除包装器。
    String REMOVE_MAPPING_EVENT = "removeMapping";

    // ------------------------------------------------------------- Properties


    /**
     * @return the available date/time for this servlet, in milliseconds since
     * the epoch.  If this date/time is in the future, any request for this
     * servlet will return an SC_SERVICE_UNAVAILABLE error.  If it is zero,
     * the servlet is currently available.  A value equal to Long.MAX_VALUE
     * is considered to mean that unavailability is permanent.
     */
    // 返回: 此servlet的可用日期/时间，以毫秒为单位。
    // 如果这个日期/时间在将来，对这个servlet的任何请求都将返回一个SC_SERVICE_UNAVAILABLE错误。
    // 如果它为零，则servlet当前可用。
    // 一个等于Long的值。MAX_VALUE表示不可用是永久的。
    long getAvailable();


    /**
     * Set the available date/time for this servlet, in milliseconds since the
     * epoch.  If this date/time is in the future, any request for this servlet
     * will return an SC_SERVICE_UNAVAILABLE error.  A value equal to
     * Long.MAX_VALUE is considered to mean that unavailability is permanent.
     *
     * @param available The new available date/time
     */
    // 为这个servlet设置可用的日期/时间，单位为纪元以来的毫秒数。
    // 如果这个日期/时间在将来，对这个servlet的任何请求都将返回一个SC_SERVICE_UNAVAILABLE错误。
    // 一个等于Long的值。MAX_VALUE表示不可用是永久的。
    void setAvailable(long available);


    /**
     * @return the load-on-startup order value (negative value means
     * load on first call).
     */
    // 返回: 启动时加载顺序值(负值表示第一次调用时加载)。
    int getLoadOnStartup();


    /**
     * Set the load-on-startup order value (negative value means
     * load on first call).
     *
     * @param value New load-on-startup value
     */
    // 设置启动时加载顺序值(负值表示第一次调用时加载)。
    // 参数: value -新的启动时加载值
    void setLoadOnStartup(int value);


    /**
     * @return the run-as identity for this servlet.
     */
    // 此servlet的run-as标识
    String getRunAs();


    /**
     * Set the run-as identity for this servlet.
     *
     * @param runAs New run-as identity value
     */
    void setRunAs(String runAs);


    /**
     * @return the fully qualified servlet class name for this servlet.
     */
    // 返回: 此servlet的完全限定servlet类名。
    String getServletClass();


    /**
     * Set the fully qualified servlet class name for this servlet.
     *
     * @param servletClass Servlet class name
     */
    // 为这个servlet设置完全限定的servlet类名。
    // 参数: servletClass—Servlet类名
    void setServletClass(String servletClass);


    /**
     * Gets the names of the methods supported by the underlying servlet.
     *
     * This is the same set of methods included in the Allow response header
     * in response to an OPTIONS request method processed by the underlying
     * servlet.
     *
     * @return Array of names of the methods supported by the underlying
     *         servlet
     *
     * @throws ServletException If the target servlet cannot be loaded
     */
    // 获取底层servlet支持的方法的名称。
    // 这与Allow响应头中包含的方法集相同，用于响应底层servlet处理的OPTIONS请求方法。
    // 返回: 底层servlet支持的方法名称的数组
    // 抛出: ServletException -如果目标servlet不能被加载
    String[] getServletMethods() throws ServletException;


    /**
     * @return <code>true</code> if this Servlet is currently unavailable.
     */
    // 如果此Servlet当前不可用，则为true
    boolean isUnavailable();


    /**
     * @return the associated Servlet instance.
     */
    // 关联的Servlet实例。
    Servlet getServlet();


    /**
     * Set the associated Servlet instance
     *
     * @param servlet The associated Servlet
     */
    // 设置相关的Servlet实例
    void setServlet(Servlet servlet);

    // --------------------------------------------------------- Public Methods


    /**
     * Add a new servlet initialization parameter for this servlet.
     *
     * @param name Name of this initialization parameter to add
     * @param value Value of this initialization parameter to add
     */
    // 为这个servlet添加一个新的servlet初始化参数。
    // 参数: name -要添加的初始化参数的名称
    // value -要添加的初始化参数的值
    void addInitParameter(String name, String value);


    /**
     * Add a mapping associated with the Wrapper.
     *
     * @param mapping The new wrapper mapping
     */
    // 添加与Wrapper关联的映射。
    // 参数: mapping——新的包装器映射
    void addMapping(String mapping);


    /**
     * Add a new security role reference record to the set of records for
     * this servlet.
     *
     * @param name Role name used within this servlet
     * @param link Role name used within the web application
     */
    void addSecurityReference(String name, String link);


    /**
     * Allocate an initialized instance of this Servlet that is ready to have
     * its <code>service()</code> method called.  If the Servlet class does
     * not implement <code>SingleThreadModel</code>, the (only) initialized
     * instance may be returned immediately.  If the Servlet class implements
     * <code>SingleThreadModel</code>, the Wrapper implementation must ensure
     * that this instance is not allocated again until it is deallocated by a
     * call to <code>deallocate()</code>.
     *
     * @exception ServletException if the Servlet init() method threw
     *  an exception
     * @exception ServletException if a loading error occurs
     * @return a new Servlet instance
     */
    // 分配这个Servlet的一个初始化实例，该实例已经准备好调用它的service()方法。
    // 如果Servlet类没有实现SingleThreadModel，(唯一的)初始化实例可能会立即返回。
    // 如果Servlet类实现了SingleThreadModel, Wrapper实现必须确保在调用deallocate()释放该实例之前不会再次分配该实例。
    // 返回: 一个新的Servlet实例
    // 抛出: ServletException——如果Servlet init()方法抛出异常
    // ServletException -如果发生加载错误
    Servlet allocate() throws ServletException;


    /**
     * Return this previously allocated servlet to the pool of available
     * instances.  If this servlet class does not implement SingleThreadModel,
     * no action is actually required.
     *
     * @param servlet The servlet to be returned
     *
     * @exception ServletException if a deallocation error occurs
     */
    // 将之前分配的servlet返回给可用实例池。
    // 如果这个servlet类没有实现SingleThreadModel，实际上不需要任何操作。
    // 参数: servlet -要返回的servlet
    // 抛出: ServletException -如果发生了释放错误
    void deallocate(Servlet servlet) throws ServletException;


    /**
     * @return the value for the specified initialization parameter name,
     * if any; otherwise return <code>null</code>.
     *
     * @param name Name of the requested initialization parameter
     */
    String findInitParameter(String name);


    /**
     * @return the names of all defined initialization parameters for this
     * servlet.
     */
    String[] findInitParameters();


    /**
     * @return the mappings associated with this wrapper.
     */
    String[] findMappings();


    /**
     * @return the security role link for the specified security role
     * reference name, if any; otherwise return <code>null</code>.
     *
     * @param name Security role reference used within this servlet
     */
    String findSecurityReference(String name);


    /**
     * @return the set of security role reference names associated with
     * this servlet, if any; otherwise return a zero-length array.
     */
    String[] findSecurityReferences();


    /**
     * Increment the error count value used when monitoring.
     */
    // 增加监视时使用的错误计数值。
    void incrementErrorCount();


    /**
     * Load and initialize an instance of this Servlet, if there is not already
     * at least one initialized instance.  This can be used, for example, to
     * load Servlets that are marked in the deployment descriptor to be loaded
     * at server startup time.
     *
     * @exception ServletException if the Servlet init() method threw
     *  an exception or if some other loading problem occurs
     */
    // 加载并初始化这个Servlet的一个实例，如果还没有至少一个初始化的实例的话。
    // 例如，这可以用于加载部署描述符中标记的servlet，以便在服务器启动时加载
    // 抛出: ServletException——如果Servlet init()方法抛出异常或发生其他加载问题
    void load() throws ServletException;


    /**
     * Remove the specified initialization parameter from this Servlet.
     *
     * @param name Name of the initialization parameter to remove
     */
    void removeInitParameter(String name);


    /**
     * Remove a mapping associated with the wrapper.
     *
     * @param mapping The pattern to remove
     */
    void removeMapping(String mapping);


    /**
     * Remove any security role reference for the specified role name.
     *
     * @param name Security role used within this servlet to be removed
     */
    void removeSecurityReference(String name);


    /**
     * Process an UnavailableException, marking this Servlet as unavailable
     * for the specified amount of time.
     *
     * @param unavailable The exception that occurred, or <code>null</code>
     *  to mark this Servlet as permanently unavailable
     */
    void unavailable(UnavailableException unavailable);


    /**
     * Unload all initialized instances of this servlet, after calling the
     * <code>destroy()</code> method for each instance.  This can be used,
     * for example, prior to shutting down the entire servlet engine, or
     * prior to reloading all of the classes from the Loader associated with
     * our Loader's repository.
     *
     * @exception ServletException if an unload error occurs
     */
    // 在为每个实例调用destroy()方法之后，卸载此servlet的所有已初始化实例。
    // 例如，在关闭整个servlet引擎之前，或者在从Loader中重新加载与Loader的存储库相关的所有类之前，可以使用这种方法。
    // 抛出: ServletException -如果发生卸载错误
    void unload() throws ServletException;


    /**
     * @return the multi-part configuration for the associated Servlet. If no
     * multi-part configuration has been defined, then <code>null</code> will be
     * returned.
     */
    MultipartConfigElement getMultipartConfigElement();


    /**
     * Set the multi-part configuration for the associated Servlet. To clear the
     * multi-part configuration specify <code>null</code> as the new value.
     *
     * @param multipartConfig The configuration associated with the Servlet
     */
    // 为关联的Servlet设置多部分配置。要清除多部分配置，请指定null作为新值。
    // 参数: multipartConfig——与Servlet关联的配置
    void setMultipartConfigElement(
            MultipartConfigElement multipartConfig);

    /**
     * Does the associated Servlet support async processing? Defaults to
     * <code>false</code>.
     *
     * @return <code>true</code> if the Servlet supports async
     */
    // 相关的Servlet支持异步处理吗?默认为false。
    // 返回: 如果Servlet支持异步，则为true
    boolean isAsyncSupported();

    /**
     * Set the async support for the associated Servlet.
     *
     * @param asyncSupport the new value
     */
    // 为关联的Servlet设置异步支持。
    // 参数: asyncSupport—新值
    void setAsyncSupported(boolean asyncSupport);

    /**
     * Is the associated Servlet enabled? Defaults to <code>true</code>.
     *
     * @return <code>true</code> if the Servlet is enabled
     */
    // 是否启用了相关的Servlet ?默认为true。
    // 返回: 如果Servlet已启用，则为true
    boolean isEnabled();

    /**
     * Sets the enabled attribute for the associated servlet.
     *
     * @param enabled the new value
     */
    void setEnabled(boolean enabled);

    /**
     * Is the Servlet overridable by a ServletContainerInitializer?
     *
     * @return <code>true</code> if the Servlet can be overridden in a ServletContainerInitializer
     */
    // ServletContainerInitializer是否重写Servlet ?
    // 返回: 如果Servlet可以在ServletContainerInitializer中被重写，则为true
    boolean isOverridable();

    /**
     * Sets the overridable attribute for this Servlet.
     *
     * @param overridable the new value
     */
    void setOverridable(boolean overridable);
}
